package com.vhans.db.service;

import cn.dev33.satoken.stp.StpUtil;
import com.vhans.core.domain.Card;
import com.vhans.db.mapper.CardMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 优惠卡片Service接口
 *
 * @author vhans
 * @date 2023-06-04
 */
@Service
public class CardService {

    @Autowired
    private CardMapper cardMapper;

    /**
     * 查询用户优惠卡片列表
     *
     * @return 优惠卡片集合
     */
    public List<Card> selectCardListByUserId() {
        int userId = StpUtil.getLoginIdAsInt();
        return cardMapper.selectCard(userId);
    }

    /**
     * 新增优惠卡片
     *
     * @param card 优惠卡片
     * @return 影响行
     */
    public int insertCard(Card card) {
        card.setUserId(StpUtil.getLoginIdAsInt());
        card.setTimeStart(LocalDateTime.now());
        card.setTimeEnd(LocalDateTime.now().plusDays(30));
        return cardMapper.insert(card);
    }

    /**
     * 更新优惠卡片价格
     */
    public int updateCard(Card card) {
        return cardMapper.updateById(Card.builder()
                .price(card.getPrice())
                .id(card.getId())
                .build());
    }
}
